/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pneuservis.frontend.web;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pneuservis.backend.services.CustomerServiceLocal;
import pneuservis.backend.services.OrderPneuServiceLocal;
import pneuservis.backend.services.ServicePneuServiceLocal;
import pneuservis.backend.services.TyreTypeServiceLocal;
import pneuservis.backend.to.CustomerTO;
import pneuservis.backend.to.OrderPneuTO;
import pneuservis.backend.to.ServicePneuTO;
import pneuservis.backend.to.TyreTypeTO;

/**
 *
 * @author Admin
 */
@UrlBinding("/orders/{$event}/{service.id}")
public class OrdersActionBean implements ActionBean {

    final static Logger log = LoggerFactory.getLogger(CustomersActionBean.class);
    @EJB(lookup = "java:global/cz.muni.fi.pa165.pneuservis_Pneuservis-ear_ear_1.0-SNAPSHOT/Pneuservis-ejb-1.0-SNAPSHOT/OrderPneuService")
    private OrderPneuServiceLocal orderPneuService;
    @EJB(lookup = "java:global/cz.muni.fi.pa165.pneuservis_Pneuservis-ear_ear_1.0-SNAPSHOT/Pneuservis-ejb-1.0-SNAPSHOT/CustomerService")
    private CustomerServiceLocal customerService;
    @EJB(lookup = "java:global/cz.muni.fi.pa165.pneuservis_Pneuservis-ear_ear_1.0-SNAPSHOT/Pneuservis-ejb-1.0-SNAPSHOT/ServicePneuService")
    private ServicePneuServiceLocal servicePneuService;
    @EJB(lookup = "java:global/cz.muni.fi.pa165.pneuservis_Pneuservis-ear_ear_1.0-SNAPSHOT/Pneuservis-ejb-1.0-SNAPSHOT/TyreTypeService")
    private TyreTypeServiceLocal tyreTypeService;
    private ActionBeanContext context;
    @ValidateNestedProperties(value = {
        @Validate(on = {"add", "save"}, field = "tyresCount", required = true, minvalue = 1),
        @Validate(on = {"add", "save"}, field = "vehicleBrand", required = true),})
    private OrderPneuTO order;
    private TyreTypeTO tyre;
    private ServicePneuTO service;
    private CustomerTO customer;
    private List<Long> checkedServices = new ArrayList<>();

    public List<Long> getCheckedServices() {
        return checkedServices;
    }

    public void setCheckedServices(List<Long> checkedServices) {
        this.checkedServices = checkedServices;
    }
    
    public TyreTypeTO getTyre() {
        return tyre;
    }

    public void setTyre(TyreTypeTO tyre) {
        this.tyre = tyre;
    }

    public ServicePneuTO getService() {
        return service;
    }

    public void setService(ServicePneuTO service) {
        this.service = service;
    }

    public CustomerTO getCustomer() {
        return customer;
    }

    public void setCustomer(CustomerTO customer) {
        this.customer = customer;
    }

    public List<TyreTypeTO> getTyres() {
        return tyreTypeService.getAllTyreTypes();
    }

    public List<CustomerTO> getCustomers() {
        return customerService.getAllCustomers();
    }

    public List<ServicePneuTO> getServices() {
        return servicePneuService.getAllServices();
    }

    public OrderPneuTO getOrder() {
        return order;
    }

    public void setOrder(OrderPneuTO order) {
        this.order = order;
    }

    public List<OrderPneuTO> getOrders() {
        return orderPneuService.getAllOrders();
    }

    @Override
    public void setContext(ActionBeanContext abc) {
        this.context = abc;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }

    @DefaultHandler
    public Resolution all() {
        log.debug("all()");
        return new ForwardResolution("/admin/showOrders.jsp");
    }

    public Resolution add() {
        log.debug("add() order={}", order);
        order.setCustomer(customer);
        order.setTyreType(tyre);
        List<ServicePneuTO> list = new ArrayList<ServicePneuTO>();
        for (Long id : checkedServices){
            list.add(servicePneuService.getService(id));
        }
        order.setServicePneus(list);
        orderPneuService.createOrder(order);
        return new RedirectResolution(this.getClass(), "all");
    }

    public Resolution delete() {
        log.debug("delete() order={}", order);
        orderPneuService.removeOrder(order);
        return new RedirectResolution(this.getClass(), "all");
    }

    public Resolution edit() {
        log.debug("edit() order={}", order);
        return new ForwardResolution("/admin/editOrder.jsp");
    }

    public Resolution save() {
        log.debug("save() order={}", order);
        orderPneuService.updateOrder(order);
        return new RedirectResolution(this.getClass(), "all");
    }

    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit","delete","save"})
    public void loadEntitiesFromDatabase() {
        String orderId = context.getRequest().getParameter("orderid");
        if (orderId != null) {
            order = orderPneuService.getOrder(Long.parseLong(orderId));
        } else {
            throw new IllegalStateException("shit happends.");
        }
    }
}
